package com.itboye.textileexpres.order;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.textileexpes.beans.MyOrderBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.util.ClickListener;
import com.itboye.textileexpres.util.PriceChange;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;

public class FragmentMyOrderAdapter extends BaseAdapter  {
	Context context;
	MyOrderBeans beans;

	public FragmentMyOrderAdapter(Context context, MyOrderBeans beans) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.beans = beans;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return beans.getList().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		viewHolder holder = null;
		if (convertView == null) {
			holder = new viewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.item_tobehipped_order, null);
			holder.area_tv = (TextView) convertView.findViewById(R.id.area_tv);
			holder.state_tv = (TextView) convertView.findViewById(R.id.state_tv);
			holder.publish_author_tv=(TextView) convertView.findViewById(R.id.publish_author_tv);
			holder.lineAdd=(LinearLayout) convertView.findViewById(R.id.lineAdd);
			convertView.setTag(holder);
		} else {
			holder = (viewHolder) convertView.getTag();
		}
		holder.publish_author_tv.setText("发布人:"+ beans.getList().get(position).getPublisher_name());
		holder.area_tv.setText(beans.getList().get(position).getNote());
		holder.state_tv.setText("买家已付款");
		holder.lineAdd.removeAllViewsInLayout();
		int lenth = beans.getList().get(position).getItems().size();
		for (int i = 0; i < lenth; i++) {
			//获取控件
			View view=LayoutInflater.from(context).inflate(R.layout.item_order_add, null);
			TextView goods_name1_tv = (TextView) view.findViewById(R.id.goods_name1_tv);
			TextView goods_name2_tv = (TextView) view.findViewById(R.id.goods_name2_tv);
			TextView goods_detail_tv = (TextView) view.findViewById(R.id.goods_detail_tv);

			TextView color_tv = (TextView) view.findViewById(R.id.color_tv);
			TextView price_tv = (TextView) view.findViewById(R.id.price_tv);
			TextView num_tv = (TextView) view.findViewById(R.id.num_tv);
			TextView total_num_tv = (TextView) view.findViewById(R.id.total_num_tv);

			TextView total_price_tv = (TextView) view.findViewById(R.id.total_price_tv);
			TextView freight_tv = (TextView) view.findViewById(R.id.freight_tv);
//			ImageView goods_img = (ImageView) convertView.findViewById(R.id.goods_img);
			ImageView img = (ImageView) view.findViewById(R.id.goods_img);
			//加载数据 15258861285
			goods_name1_tv.setText(beans.getList().get(position).getItems().get(i).getOrder_code());
			goods_name2_tv.setText(beans.getList().get(position).getItems().get(i).getName());  
			goods_detail_tv.setText(beans.getList().get(position).getItems().get(i).getName());

			color_tv.setText(beans.getList().get(position).getItems().get(i).getSku_desc());
			price_tv.setText(beans.getList().get(position).getItems().get(i).getOri_price());
			num_tv.setText("X"+beans.getList().get(position).getItems().get(i).getCount());

			total_num_tv.setText("共"+ beans.getList().get(position).getItems().get(i).getDt_goods_unit() + "米");
			total_price_tv.setText("合计"+ PriceChange.Change(beans.getList().get(position).getItems().get(i).getPrice()) + "元");
			goods_detail_tv.setText(beans.getList().get(position).getItems().get(i).getDt_goods_unit());
			freight_tv.setText("(包含运费"+beans.getList().get(position).getItems().get(i).getWeight()+"元)");
			
			XImageLoader.load(
					GlobalConfig.IMAGE_DOUBLE
							+ beans.getList().get(position).getItems().get(i)
									.getImg(), img);
			MarginLayoutParams layoutParams = new MarginLayoutParams(
					MarginLayoutParams.WRAP_CONTENT,
					MarginLayoutParams.WRAP_CONTENT);
			layoutParams.setMargins(5, 5, 5, 5);
	
			holder.lineAdd.addView(view, layoutParams);
			
		}
		holder.lineAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent;
				intent = new Intent(context, OrderDetailActivityFahuo.class);
				intent.putExtra("order_code", beans.getList().get(position)
						.getOrder_code());
				intent.putExtra("str", beans.getList().get(position)
						.getQuery_status());
				context.startActivity(intent);
			}
		});
		//
		return convertView;
	}

	class viewHolder {
		private TextView area_tv;
		private TextView state_tv;
		private TextView publish_author_tv;
		private LinearLayout lineAdd;
		

	}


}
